#!E:/Python/Python38/python.exe
# -*- coding: utf-8 -*-

import numpy as np

from PySide2.QtCore import *
from PySide2.QtCharts import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *

from ui_chartswidget import Ui_ChartsWidget


class ChartsWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_ChartsWidget()
        self.ui.setupUi(self)

        self.x_value = 0
        self.x_step = 0
        self.point_cnt = 100

        self.init_charts()

        self.timer = QTimer(self)
        self.timer.setInterval(100)
        self.timer.timeout.connect(self.update_charts)

        self.ui.btnStart.setDisabled(False)
        self.ui.btnStop.setDisabled(True)
        self.ui.btnStart.clicked.connect(self.start)
        self.ui.btnStop.clicked.connect(self.stop)

    def init_charts(self):
        self.series_sin = QtCharts.QLineSeries()
        self.series_cos = QtCharts.QLineSeries()
        self.series_sin.setName("y=sin(x)")
        self.series_cos.setName("y=cos(x)")
        x_start, x_stop = 0.0, 2*np.pi
        self.x_step = (x_stop-x_start)/(self.point_cnt-1)
        self.x_value = x_stop
        xs = np.linspace(x_start, x_stop, self.point_cnt)
        ys_sin, ys_cos = np.sin(xs), np.cos(xs)
        for i in range(self.point_cnt):
            self.series_sin.append(xs[i], ys_sin[i])
            self.series_cos.append(xs[i], ys_cos[i])
        # qDebug('init size of sin: %d' % self.series_sin.count())
        # qDebug('init size of cos: %d' % self.series_cos.count())

        self.chart = QtCharts.QChart()
        self.chart.addSeries(self.series_sin)
        self.chart.addSeries(self.series_cos)
        self.chart.setTitle('QtCharts Test')

        self.axis_x = QtCharts.QValueAxis()
        self.axis_x.setLabelFormat('%.1f')
        self.axis_x.setTitleText('X')
        self.axis_x.setTickCount(11)
        self.axis_x.setMinorTickCount(4)
        self.chart.addAxis(self.axis_x, Qt.AlignBottom)
        self.series_sin.attachAxis(self.axis_x)
        self.series_cos.attachAxis(self.axis_x)

        self.axis_y = QtCharts.QValueAxis()
        self.axis_y.setLabelFormat('%.1f')
        self.axis_y.setTitleText('Y')
        self.chart.addAxis(self.axis_y, Qt.AlignLeft)
        self.series_sin.attachAxis(self.axis_y)
        self.series_cos.attachAxis(self.axis_y)

        self.chart_view = QtCharts.QChartView(self.chart, self)
        self.chart_view.setRenderHint(QPainter.Antialiasing)

        self.horizontalLayout = QHBoxLayout(self.ui.frameCharts)
        self.horizontalLayout.addWidget(self.chart_view)

    @Slot()
    def start(self):
        self.ui.btnStart.setDisabled(True)
        self.ui.btnStop.setDisabled(False)
        self.timer.start()

    @Slot()
    def stop(self):
        self.ui.btnStart.setDisabled(False)
        self.ui.btnStop.setDisabled(True)
        self.timer.stop()

    @Slot()
    def update_charts(self):
        self.series_sin.remove(0)
        self.series_cos.remove(0)
        self.x_value += self.x_step
        self.series_sin.append(self.x_value, np.sin(self.x_value))
        self.series_cos.append(self.x_value, np.cos(self.x_value))
        # qDebug('current size of sin: %d' % self.series_sin.count())
        # qDebug('current size of cos: %d' % self.series_cos.count())
        self.chart.scroll(self.chart.plotArea().width()/(self.point_cnt-1), 0)

    def closeEvent(self, event):
        if (self.timer.isActive()):
            self.timer.stop()
        return super().closeEvent(event)